package com.huaiwei.usmManager.modules.usm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.R;
import com.huaiwei.usmManager.modules.usm.entity.GoodsEntity;
import com.huaiwei.usmManager.modules.usm.entity.GoodsSpecEntity;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import com.huaiwei.usmManager.modules.usm.service.GoodsSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goodsSpec")
@Api(tags = "商品规格管理")
public class GoodsSpecController {
    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private GoodsService goodsService;


    @PostMapping("/update")
    @ApiOperation("添加或修改商品规格")
    public R update(@RequestBody GoodsSpecEntity goodsSpecEntity){
        goodsSpecService.saveOrUpdate(goodsSpecEntity);
        return R.ok();
    }

    @DeleteMapping("/delete/{prospecId}")
    @ApiOperation("根据id删除商品规格")
    public R delete(@PathVariable("prospecId") Integer prospecId){
        if(goodsSpecService.getCountBySpecId(prospecId)){
            goodsSpecService.removeById(prospecId);
            return R.ok();
        }else{
            return R.error("该商品规格有商品使用");
        }
    }


    @GetMapping("/list")
    @ApiOperation("分页查询商品列表")
    public R list(@RequestParam Map<String,Object> params){
        return R.ok().put("page",goodsSpecService.getPage(params));
    }

    @GetMapping("/select/{id}")
    @ApiOperation("根据id查找")
    public R select(@PathVariable("id") Integer id){
        return R.ok().put("data",goodsSpecService.getById(id));
    }

    @GetMapping("/showGoods/{id}")
    @ApiOperation("查询拥有此规格的商品")
    public R show(@PathVariable("id") Integer id,@RequestParam Map<String,Object> params){
        return R.ok().put("list",goodsSpecService.getGoodsBySpecId(id,params));
    }
}
